/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nuce.bo;

import dataaccess.DataAccess;
import nuce.entity.Question;

/**
 *
 * @author LINHNC
 */
public class QuestionBO {

    public int insert(Question question) {
        final String procName = "Question_Insert";
        try {
            dataaccess.DataAccess da = new DataAccess();
            String[] args = new String[6];
            args[0] = question.getContent();
            args[1] = question.getAnswer1();
            args[2] = question.getAnswer2();
            args[3] = question.getAnswer3();
            args[4] = question.getAnswer4();
            args[5] = Integer.toString(question.getLevel());
            da.executeProcVoid(procName, args);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    public int update(Question question) {
        final String procName = "Question_Update";
        try {
            dataaccess.DataAccess da = new DataAccess();
            String[] args = new String[7];
            args[0] = Integer.toString(question.getId());
            args[1] = question.getContent();
            args[2] = question.getAnswer1();
            args[3] = question.getAnswer2();
            args[4] = question.getAnswer3();
            args[5] = question.getAnswer4();
            args[6] = Integer.toString(question.getLevel());
            da.executeProc(procName, args);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    public int deleteById(int Id) {
        final String procName = "Question_DeleteById";
        try {
            dataaccess.DataAccess da = new DataAccess();
            String[] args = new String[1];
            args[0] = Integer.toString(Id);
            da.executeProcVoid(procName, args);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    public void swap(Question q, int a, int b) {
        if (a >= 0 && a < 4 && b >= 0 && b < 4) {
            String ansSource = "answer1";
            String ansDes = "answer1";
            switch (a) {
                case 0:
                    ansSource = q.getAnswer1();
                    break;
                case 1:
                    ansSource = q.getAnswer2();
                    break;
                case 2:
                    ansSource = q.getAnswer3();
                    break;
                case 3:
                    ansSource = q.getAnswer4();
                    break;
            }
            switch (b) {
                case 0:
                    ansDes = q.getAnswer1();
                    break;
                case 1:
                    ansDes = q.getAnswer2();
                    break;
                case 2:
                    ansDes = q.getAnswer3();
                    break;
                case 3:
                    ansDes = q.getAnswer4();
                    break;
            }

            switch (a) {
                case 0:
                    q.setAnswer1(ansDes);
                    break;
                case 1:
                    q.setAnswer2(ansDes);
                    break;
                case 2:
                    q.setAnswer3(ansDes);
                    break;
                case 3:
                    q.setAnswer4(ansDes);
                    break;
            }
            switch (b) {
                case 0:
                    q.setAnswer1(ansSource);
                    break;
                case 1:
                    q.setAnswer2(ansSource);
                    break;
                case 2:
                    q.setAnswer3(ansSource);
                    break;
                case 3:
                    q.setAnswer4(ansSource);
                    break;
            }
        }
    }
}
